package cn.wuyu.baselibrary.utils

import android.content.Context
import android.content.DialogInterface
import android.text.TextUtils
import android.view.KeyEvent
import androidx.appcompat.app.AlertDialog
import cn.wuyu.baselibrary.R

class DialogHelper {

    fun showRationaleDialog(shouldRequest: PermissionUtils.OnRationaleListener.ShouldRequest) {
        val topActivity = ActivityUtils.getTopActivity()
        if (topActivity == null || topActivity.isFinishing) return
        AlertDialog.Builder(topActivity)
                .setTitle(R.string.dialog_alert_title)
                .setMessage(R.string.permission_rationale_message)
                .setPositiveButton("好的") { _, which -> shouldRequest.again(true) }
                .setNegativeButton("取消") { dialog, which -> shouldRequest.again(false) }
                .setCancelable(false)
                .create()
                .show()

    }

    fun showOpenAppSettingDialog() {
        val topActivity = ActivityUtils.getTopActivity()
        if (topActivity == null || topActivity.isFinishing) return
        AlertDialog.Builder(topActivity)
                .setTitle(android.R.string.dialog_alert_title)
                .setMessage(R.string.permission_denied_forever_message)
                .setPositiveButton(android.R.string.ok) { dialog, which -> PermissionUtils.launchAppDetailsSettings() }
                .setNegativeButton(android.R.string.cancel) { dialog, which -> }
                .setCancelable(false)
                .create()
                .show()
    }

    constructor() {}
    constructor(context: Context) {
        this.context = context
    }

    companion object {
        fun with(context: Context): DialogHelper {
            return DialogHelper(context)
        }

        fun with(): DialogHelper {
            return DialogHelper()
        }
    }

    fun setHintMsg(hintMsg: String): DialogHelper {
        this.hintMsg = hintMsg
        return this
    }

    fun setHintMsg(resId: Int): DialogHelper {
        this.hintMsg = ResUtils.getString(resId)
        return this
    }

    fun setTitle(title: String): DialogHelper {
        this.title = title
        return this
    }

    fun setTitle(resId: Int): DialogHelper {
        this.title = ResUtils.getString(resId)
        return this
    }

    fun setTitleVisible(isTitleVisible: Boolean): DialogHelper {
        this.isTitleVisible = isTitleVisible
        return this
    }

    fun setConfirmClickListener(confirmListener: DialogInterface.OnClickListener): DialogHelper {
        this.confirmListener = confirmListener
        return this
    }

    fun setCancelListener(cancelListener: DialogInterface.OnClickListener): DialogHelper {
        this.cancelListener = cancelListener
        return this
    }

    fun setConfirmVisible(confirmVisible: Boolean): DialogHelper {
        this.isConfirmVisible = confirmVisible
        return this
    }

    fun setCancelVisible(cancelVisible: Boolean): DialogHelper {
        this.isCancelVisible = cancelVisible
        return this
    }

    fun setConfirmText(confirmText: String): DialogHelper {
        this.confirmText = confirmText
        return this
    }

    fun setCancelText(cancelText: String): DialogHelper {
        this.cancelText = cancelText
        return this
    }

    fun setInputMode(isInputMode: Boolean): DialogHelper {
        this.isInputMode = isInputMode
        return this
    }

    private var context: Context? = null
    private var confirmListener: DialogInterface.OnClickListener? = null
    private var cancelListener: DialogInterface.OnClickListener? = null
    private var title: String? = null
    private var hintMsg: String? = null
    private var confirmText: String? = null
    private var cancelText: String? = null
    private var isInputMode = false
    private var isTitleVisible = true
    private var isConfirmVisible = true
    private var isCancelVisible = true

    fun show() {
        val builder = AlertDialog.Builder(context!!)
        if (isTitleVisible && !TextUtils.isEmpty(title)) {
            builder.setTitle(title)
        } else {
            builder.setTitle(R.string.dialog_alert_title)
        }
        if (!TextUtils.isEmpty(hintMsg)) {
            builder.setMessage(hintMsg)
        }
        if (isConfirmVisible) {
            if(!TextUtils.isEmpty(confirmText)){
                builder.setPositiveButton(confirmText, confirmListener)
            } else{
                builder.setPositiveButton(R.string.dialog_btn_confirm_text, confirmListener)
            }
        }
        if (isCancelVisible) {
            if(!TextUtils.isEmpty(confirmText)){
                builder.setNegativeButton(cancelText, cancelListener)
            } else{
                builder.setNegativeButton(R.string.dialog_btn_cancel_text, cancelListener)
            }
        }
        builder.setOnKeyListener(DialogInterface.OnKeyListener { _, keyCode, event ->
            if (keyCode == KeyEvent.KEYCODE_BACK && event?.action == KeyEvent.ACTION_DOWN) {
                return@OnKeyListener true
            }
            false
        })
        builder.setCancelable(false)
        val alertDialog = builder.create()
        alertDialog.show()
    }
}
